/**
 * 
 */
package com.maywide.jdp.system.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.maywide.jdp.core.vo.Page;
import com.maywide.jdp.system.annotation.AccessChecked;
import com.maywide.jdp.system.annotation.JSON;
import com.maywide.jdp.system.bean.OrganizationBean;
import com.maywide.jdp.system.entities.Organization;
import com.maywide.jdp.system.utils.JSONEditor;
import com.maywide.jdp.system.vo.OrganizationTree;

/**
 * 机构管理Service
 * 
 * @author luoyang
 * @version 1.0
 * @since JDK1.6
 */
@Controller("organizationService")
public class OrganizationServiceImpl implements OrganizationService {
	/**
	 * 机构Bean接口
	 */
	private OrganizationBean bean = null;

	@Autowired
	@Qualifier("organizationBean")
	public void setBean(OrganizationBean bean) {
		this.bean = bean;
	}

	@JSON
	@RequestMapping(value = "/system/organization/insert.json")
	@Override
	public Organization insert(@RequestParam(value = "organization")Organization organization,
			HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		bean.insert(organization);
		return organization;
	}

	@AccessChecked
	@JSON
	@RequestMapping(value = "/system/organization/update.json")
	@Override
	public Organization update(@RequestParam(value = "organization")Organization organizationVO,
			HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		bean.update(organizationVO);
		return organizationVO;
	}

	@JSON
	@RequestMapping(value = "/system/organization/updateApp.json")
	@Override
	public Organization updateApp(
			@RequestParam(value = "organization")Organization organizationVO, 
			@RequestParam(value = "appType")String appType,
			HttpServletRequest request,
			HttpServletResponse response, 
			HttpSession session) throws Exception {
		bean.updateApp(organizationVO, appType);
		return organizationVO;
	}

	@JSON
	@RequestMapping(value = "/system/organization/load.json")
	@Override
	public Organization load(@RequestParam(value = "id")String id, HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws Exception {
		Organization organization = (Organization) bean.load(id);
		return organization;
	}

	@RequestMapping(value = "/system/organization/lock.json")
	@Override
	public void lock(@RequestParam(value = "id")String id, HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		bean.lock(id);
	}

	@RequestMapping(value = "/system/organization/unlock.json")
	@Override
	public void unlock(@RequestParam(value = "id")String id, HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws Exception {
		bean.unlock(id);
	}

	@RequestMapping(value = "/system/organization/move.json")
	@Override
	public void move(
			@RequestParam(value = "ids")List<String> ids, 
			@RequestParam(value = "targetID")String targetID,
			HttpServletRequest request,
			HttpServletResponse response, 
			HttpSession session) throws Exception {
		bean.move(ids, targetID);
	}

	@JSON
	@RequestMapping("/system/organization/tree.json")
	@SuppressWarnings("unchecked")
	public List<OrganizationTree> tree(
			@RequestParam(value = "parentID")String parentID,
			@RequestParam(value = "rootID",required = false)String rootID, 
			HttpServletRequest request,
			HttpServletResponse response, 
			HttpSession session) throws Exception {
		Map<String, Object> conditions = new HashMap<String, Object>();
		if ("org_root".equals(parentID) || "oper_org_root".equals(parentID)
				|| "role_org_root".equals(parentID) || "param_org_root".equals(parentID))
			parentID = null;
		
		if (rootID != null && parentID == null) {
			conditions.put("rootID", rootID);
		} else {
			if (parentID != null)
				conditions.put("parentID", parentID);
			else
				conditions.put("noParentID", null);
		}
		
		return bean.tree(conditions);
	}

	@JSON
	@RequestMapping("/system/organization/list.json")
	public List<Organization> list(
			@RequestParam(value = "sort",defaultValue = "o.code")String sort, 
			@RequestParam(value = "dir",defaultValue = "asc")String dir, 
			@RequestParam(value = "operatorID",required = false)String operatorID, 
			@RequestParam(value = "rootID",required = false)String rootID, 
			@RequestParam(value = "active",required = false)String active,
			HttpServletRequest request, 
			HttpServletResponse response,
			HttpSession session) throws Exception {
		Map<String, Object> condtions = new HashMap<String, Object>();

		if (operatorID != null)
			condtions.put("operatorID", operatorID);
		if (rootID != null)
			condtions.put("rootID", rootID);
		if (active != null)
			condtions.put("active", Boolean.parseBoolean(active));

		return bean.list(sort,dir,condtions, null);
	}

//	@AccessChecked(true)
	@JSON
	@RequestMapping("/system/organization/page.json")
	public Page page(
			@RequestParam(value = "first", defaultValue = "0") int first,
			@RequestParam(value = "limit", defaultValue = "20") int limit,
			@RequestParam(value = "sort", defaultValue = "o.code")String sort, 
			@RequestParam(value = "dir", defaultValue = "ASC")String dir, 
			@RequestParam(value = "parentID", required = false)String parentID, 
			@RequestParam(value = "key", required = false)String key,
			@RequestParam(value = "active", required = false)String active, 
			@RequestParam(value = "rootID", required = false)String rootID, 
			@RequestParam(value = "code", required = false)String code, 
			@RequestParam(value = "name", required = false)String name,
			@RequestParam(value = "principal", required = false)String principal, 
			@RequestParam(value = "mobile", required = false)String mobile, 
			@RequestParam(value = "phone", required = false)String phone,
			HttpServletRequest request, 
			HttpServletResponse response,
			HttpSession session) throws Exception {
		Map<String, Object> conditions = new HashMap<String, Object>();
		if (key != null) {
			key = new StringBuffer("%").append(key).append("%").toString();
			conditions.put("key", key);
		}

		if (active != null)
			conditions.put("active", Boolean.parseBoolean(active));

		if ("org_root".equals(parentID))
			parentID = null;
		if (rootID != null && parentID == null) {
			conditions.put("rootID", rootID);
		} else {
			if (parentID != null)
				conditions.put("parentID", parentID);
			else
				conditions.put("noParentID", null);
		}

		if (code != null) {
			code = new StringBuffer("%").append(code).append("%").toString();
			conditions.put("code", code);
		}
		if (name != null) {
			name = new StringBuffer("%").append(name).append("%").toString();
			conditions.put("name", name);
		}
		if (principal != null) {
			principal = new StringBuffer("%").append(principal).append("%")
					.toString();
			conditions.put("principal", principal);
		}
		if (mobile != null) {
			mobile = new StringBuffer("%").append(mobile).append("%")
					.toString();
			conditions.put("mobile", mobile);
		}
		if (phone != null) {
			phone = new StringBuffer("%").append(phone).append("%").toString();
			conditions.put("phone", phone);
		}
		
		return bean.page(first, limit, sort, dir, conditions, null);
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Organization.class, new JSONEditor(Organization.class));
	}
}
